﻿using System.IO;
using System.Xml.Serialization;
using ProjectWizardManagement.Entity.Model;
using ProjectWizardManagementv1.Infrastructure.Abstract;

namespace ProjectWizardManagementv1.Infrastructure.Concrete
{
    public class XmlIO : IProjectIO
    {
        public Project DeserializeFromInput(string path)
        {
            var deserializer = new XmlSerializer(typeof(Project));
            Project project = null;

            if(File.Exists(path))
            {
                using(TextReader reader = new StreamReader(path))
                {
                    project = (Project) deserializer.Deserialize(reader);
                } 
            }

            return project;
        }

        public void SerializeToOutput(string path, Project project)
        {
            var serializer = new XmlSerializer(typeof (Project));
            using(TextWriter writer = new StreamWriter(path))
            {
                serializer.Serialize(writer, project);
            }
        }
    }
}
